package com.tycg.muyu.chat.controller;

import com.tycg.muyu.chat.pojo.MUser;
import com.tycg.muyu.chat.service.UserService;
import com.tycg.muyu.commons.entity.Result;
import com.tycg.muyu.commons.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by xiaowu on 2020/2/20
 */
@RequestMapping("/user")
@RestController
public class UserController {


    @Autowired
    private UserService userService;

    /** 查询全部用户 */
    @GetMapping("/findAll")
    public Result findAll(){
        List<MUser> users = userService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", users);
    }

    /**
     * 根据id查询用户信息
     */
    @PostMapping("/findById/{id}")
    public Result findById(@PathVariable Long id) {

        MUser user = userService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", user);
    }
    /**
     * 添加用户
     */
    @PostMapping("/add")
    public Result add(MUser user) {

        userService.add(user);
        return new Result(true, StatusCode.OK,"添加用户成功");
    }

    @GetMapping("/find")
    public String find(){

        return " 找到了";
    }

    /**
     * 删除用户
     */
    @PostMapping("/del/{id}")
    public Result del(@PathVariable Long id) {

        userService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/update")
    public Result update(MUser user) {

        userService.update(user);
        return new Result(true, StatusCode.OK, "修改用户信息成功");
    }
}
